<template>
  <text
    :style="{ width: iconSize, height: iconSize }"
    class="com-colored-icon"
    :class="['com-colored-icon-' + name, customPrefix, customPrefix ? name : '']"
    @click="_onClick"
  />
</template>

<script>
const getVal = (val) => {
  const reg = /^[0-9]*$/g
  return typeof val === 'number' || reg.test(val) ? val + 'px' : val
}

/**
 * Icons 彩色图标 (需用iconfont-tools转下)
 * @description 用于展示彩色 icons 图标
 * @property {Number} size 图标大小
 * @property {String} name 图标图案
 * @property {String} customPrefix 自定义图标
 * @event {Function} click 点击 Icon 触发事件
 */
export default {
  name: 'Com-Colored-Icon',
  emits: ['click'],
  props: {
    name: {
      type: String,
      default: '',
    },
    size: {
      type: [Number, String],
      default: 16,
    },
    customPrefix: {
      type: String,
      default: '',
    },
    stop: {
      type: Boolean,
      default: true,
    },
  },
  computed: {
    iconSize() {
      return getVal(this.size)
    },
  },
  methods: {
    _onClick(e) {
      this.$emit('click')
      if (!this.stop && e) {
        e.stopPropagation()
      }
    },
  },
}
</script>

<style lang="scss">
@import './iconfont-colored.css';
</style>
